package com.taokatao.cardmee.business;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taokatao.cardmee.App;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.BusEvent;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.api.ApiManager;
import com.taokatao.cardmee.api.RespInfo;
import com.taokatao.cardmee.api.RespWrapper;
import com.taokatao.cardmee.api.SubscriberAdapter;
import com.taokatao.cardmee.business.cardcrud.InfoEditActivity;
import com.taokatao.cardmee.db.UserDao;
import com.taokatao.cardmee.db.model.Card;
import com.taokatao.cardmee.db.model.User;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class CommonReceiveInfoActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.t_submit) TextView tSubmit;
    @BindView(R.id.l_realname) View lRealname;
    @BindView(R.id.t_realname) TextView tRealName;
    @BindView(R.id.i_background) ImageView iBackground;
    @BindView(R.id.i_logo) ImageView iLogo;
    @BindView(R.id.t_grade) TextView tGrade;
    @BindView(R.id.t_card_name) TextView tCardName;
    @BindView(R.id.i_code) ImageView iCode;
    @BindView(R.id.t_number) TextView tNumber;

    private int styleColor;
    private Card c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_receive_info);
        ButterKnife.bind(this);

        c = (Card) getIntent().getSerializableExtra("extra_card_info");
        resetBasicInfo();

        tRealName.setText(App.i().getCurrUser().getRealName());
    }


    private void resetBasicInfo() {
        if (!TextUtils.isEmpty(c.getBackColor()) && c.getBackColor().length() == 7 && c.getBackColor().startsWith("#")) {
            styleColor = Color.parseColor(c.getBackColor());
        } else {
            styleColor = 0xFF4C4C4C;
        }
        tCardName.setText(c.getBusinessName());
        if (!TextUtils.isEmpty(c.getSname())) {
            tCardName.setText(c.getSname());
        }

        if (!TextUtils.isEmpty(c.getMCardIcon())) {
            iLogo.setVisibility(View.VISIBLE);
            Glide.with(iLogo.getContext()).load(c.getMCardIcon()).into(iLogo);
        }


        boolean blackColorStyle = Utils.isDarkColorTheme(styleColor);
        if (!blackColorStyle) {
            styleColor = 0xFF242424;
        }
        iBackground.setImageDrawable(new ColorDrawable(styleColor));
        iCode.setImageResource(blackColorStyle ? R.mipmap.card_code_white : R.mipmap.card_code_black);
        String cardNumber = TextUtils.isEmpty(c.getMCardNo()) ? "" : c.getMCardNo();
        tNumber.setText("NO." + Utils.formatCardCode(cardNumber));

        if (!TextUtils.isEmpty(c.getGradeName()) && !TextUtils.equals("普通卡", c.getGradeName())) {
            tGrade.setVisibility(View.VISIBLE);
            tGrade.setText(c.getGradeName());
        } else {
            tGrade.setVisibility(View.GONE);
        }
    }

    static final int REQUEST_CODE_PROFILE_EDIT_REALNAME = 1033;

    @Override
    @OnClick({R.id.t_submit ,R.id.l_realname})
    public void onClick(View v) {
        if (v == tSubmit) {
            tSubmit.setEnabled(false);
            requestScanToAddCardConfirm(c.getMCardId());
        } else if (v == lRealname) {
            Intent intent = new Intent(this, InfoEditActivity.class);
            intent.putExtra("extra_content", tRealName.getText().toString());
            intent.putExtra("extra_max_length", 10);
            startActivityForResult(intent, REQUEST_CODE_PROFILE_EDIT_REALNAME);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PROFILE_EDIT_REALNAME) {
            if (resultCode == RESULT_OK) {
                String result = data.getStringExtra("result");
                tRealName.setText(result);
            }
        }
    }

    private void requestScanToAddCardConfirm(long cardId) {
        final String mInputRealName = tRealName.getText().toString();
        ApiManager.i().scanToAddCardConfirm(this, App.i().getCurrUser().getUuid(), cardId, mInputRealName, new SubscriberAdapter<RespWrapper<RespInfo>>() {
            @Override
            public void onNext(RespWrapper<RespInfo> o) {
                super.onNext(o);
                if (o.code == C.http.RESULT_CODE_SUCCESS) {
                    Utils.postMainThreadBusEvent(BusEvent.CARD_REFRESH);
                    Intent intent = new Intent(CommonReceiveInfoActivity.this, CommonReceiveCompleteActivity.class);
                    startActivity(intent);

                    // 更新用户真实姓名
                    User user = App.i().getCurrUser();
                    user.setRealName(mInputRealName);
                    UserDao userDao = App.i().getUserDBSession().getUserDao();
                    userDao.insertOrReplace(user);

                    finish();
                }
            }
        }.showProgress(this).submitDisable(tSubmit));

    }
}
